<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/6/11
 * Time: 16:03
 */

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use yii\web\Session;
use yii\redis\get;
use \common\helps\tools;//使用工具类

/**
 * Login controller
 */
class LoginController extends Controller
{

    // 视图类实例
    protected $view;
    // token
    protected $token;

    public $user = [];
    /**
     * @var 令牌盐值
     */
    protected $accessTokenStr = 'heikezhidao';



    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }


    /**
     * title  登录
     * @return string
     */
    public function actionLogin()
    {
        return $this->render('login');
    }

    /**
     * title  登录验证
     * @return string
     */
    public function actionChecklogin()
    {
        header('Access-Control-Allow-Origin:*');

        $query = new \yii\db\Query();
        $data = [];
        $post = Yii::$app->request->post();
        $username = $post['uname'];
        $password = $post['upwd'];
//        $username = '15864078647';
//        $password = '123456';

        $u = $query
            ->select(['id'])
            ->from('ifssc_php.t_s_user')
            ->where(['mobilePhone' => $username])
            ->one();
        if(empty($u)){
            return tools::ajax_return('4','用户名不存在',$data);
        }

        $user = $query
            ->select(['password','realname','username'])
            ->from('ifssc_php.t_s_base_user')
            ->where(['ID' => $u['id']])
            ->one();
        if(!$user){
            return tools::ajax_return('4','用户名不存在',$data);
        }

        //token
        $this->token = sha1($user['username'].$user['password'].$this->accessTokenStr).time();


        if($user['password'] == $password){
            $this->user = ['username'=>$u['id'],'password'=>$password,'realname'=>$user['realname'],'id'=>$u['id'],'token'=> $this->token];
            //存储用户信息
            $redis = Yii::$app->cache;
            $redis->set($u['id'],$this->user);
//            $a = $redis->get($u['id'].'_info');


            //返回前端
            $project_info = $query
                ->select(['id','project_no','project_name'])
                ->from('ifssc_php.project_info')
                ->where(['project_head_id' => $u['id']])
                ->orderBy('id DESC')
                ->one();

            //部门
            $org = $query
                ->select(['ID','org_id'])
                ->from('ifssc_php.t_s_user_org')
                ->where(['user_id' => $u['id']])
                ->one();

            $depart = $query
                ->select(['ID','departname','org_code','org_type'])
                ->from('ifssc_php.t_s_depart')
                ->where(['ID' => $org['org_id']])
                ->one();

            $list = tools::familyTree($depart,$depart['org_code']);
            $list = array_reverse($list);



            $data = $user;
            $data['id'] = $u['id'];
            $data['item_id'] = $project_info['id'];
            $data['project_no'] = $project_info['project_no'];
            $data['organization'] = $list;
            $data['token'] = $this->token;

            return tools::ajax_return('0','成功',$data);

        }else{
            return tools::ajax_return_error('3','失败',[]);
        }

    }


    /**
     * title 退出登录
     */

    public function actionLoginout(){
        header('Access-Control-Allow-Origin:*');

        $post = Yii::$app->request->post();
        $id = $post['id'];

        $redis = Yii::$app->cache;
        $out = $redis->delete($id);

        if($out){
            return tools::ajax_return('0','成功',[]);
        }else{
            return tools::ajax_return_error('1','失败',[]);
        }

    }




}